﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LogisticDatabase.Models;
using LogisticDatabase.Data;

namespace LogisticDatabase
{
    public class OrderDataAccess : IDataAccess<Order>
    {
        public List<Order> listAll()
        {
         
            List<Order> orders = new List<Order>();
            using (LogisticDatabaseEntity ctx = new LogisticDatabaseEntity())
            {
               
                orders = ctx.tblOrders.Include("tblFactory").Include("tblProduct")
                    .Select(t => new Order { Id = t.OrderID, CustomerName = t.OrderCustomerName, 
                        Product=new Product { Id = t.tblProduct.ProductID, 
                            Name = t.tblProduct.ProductName,
                            Price = t.tblProduct.ProductPrice,
                            Factory = new Factory {
                                Id =  t.tblProduct.tblFactory.FactoryID,
                                Name = t.tblProduct.tblFactory.FactoryName

                                }
                         
                           }, 
                    Date = t.OrderDate, 
                    Quanity = t.OrderQuantity, 
                    StateText = t.OrderState                    
                
                }).ToList();

            }
            return orders;

        }
    
     public void edit(Order order)
       {
            using (LogisticDatabaseEntity ctx = new LogisticDatabaseEntity())
            {
                var c = ctx.tblOrders.FirstOrDefault(t => t.OrderID == order.Id);
                if (c != null)
                {
                    c.OrderCustomerName = order.CustomerName;
                    c.OrderProduct = order.Product.Id;
                    c.OrderDate = order.Date;
                    c.OrderQuantity = order.Quanity;
                    c.OrderState = Order.OrderStateToString(order.State);

                    ctx.SaveChanges();
                }


              
            }
        }
        public void add(Order orders)
        {
            using (LogisticDatabaseEntity ctx = new LogisticDatabaseEntity())
            {
                tblOrders tc = new tblOrders();

                tc.OrderCustomerName = orders.CustomerName;
                tc.OrderProduct = orders.Product.Id;
                tc.OrderDate = orders.Date;
                tc.OrderQuantity = orders.Quanity;
                tc.OrderState = Order.OrderStateToString(orders.State);

                ctx.tblOrders.Add(tc);

                ctx.SaveChanges();
            }
        }
        public void delete(Order order)
        {
            using (LogisticDatabaseEntity ctx = new LogisticDatabaseEntity())
            {
                var c = ctx.tblOrders.FirstOrDefault(t => t.OrderID == order.Id);
                if (c != null)
                {
                    ctx.tblOrders.Remove(c);
                    ctx.SaveChanges();
                }
            }

        }

    }
}
